package com.start.data.screen.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.common.api.Result;
import com.start.common.dataRule.DataQueryConstructor;
import com.start.common.entity.StartBaseEntity;
import com.start.core.annotations.RegisterMenu;
import com.start.core.annotations.RegisterPermission;
import com.start.core.annotations.StartLog;
import com.start.data.screen.entity.DataScreenConfig;
import com.start.data.screen.service.DataScreenConfigService;
import com.start.security.annotations.DataPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/dataScreen/config")
@RegisterMenu(value = "数据大屏配置详情", systemCode = "start:data:screen:config", component = "views/start/data/config/index", path = "data/config/index")
public class DataScreenConfigController {
    private static final String TITLE = "数据大屏配置管理";

    @Autowired
    private DataScreenConfigService sysFormConfigService;


    @GetMapping("/page")
    @StartLog(value = TITLE, action = "列表查询")
    @DataPermission(value = "views/start/data/screen/index")
    public Result<?> page(Page<DataScreenConfig> page, DataScreenConfig entity) {
        QueryWrapper<DataScreenConfig> lambdaQuery = DataQueryConstructor.createEntity(entity);
        LambdaQueryWrapper<DataScreenConfig> lambda = lambdaQuery.lambda();
        lambda.orderByDesc(StartBaseEntity::getCreateTime);
        return Result.ok(sysFormConfigService.page(page, lambda));
    }

    @PostMapping("/add")
    @StartLog(value = TITLE, action = "添加")
    @PreAuthorize("@pas.hasPermission('start:data:screen:config:add')")
    @RegisterPermission(value = "start:data:screen:config:add", name = "添加")
    public Result<?> add(@RequestBody DataScreenConfig entity) {
        return Result.ok(entity.getId());
    }

    @PutMapping("/updateById")
    @StartLog(value = TITLE, action = "修改")
    @PreAuthorize("@pas.hasPermission('start:data:screen:config:edit')")
    @RegisterPermission(value = "start:data:screen:config:edit", name = "修改")
    public Result<?> updateById(@RequestBody DataScreenConfig entity) {
        sysFormConfigService.updateById(entity);
        return Result.ok();
    }


    @GetMapping("/getById/{id}")
    @StartLog(value = TITLE, action = "查看")
    @PreAuthorize("@pas.hasPermission('start:data:screen:config:info')")
    @RegisterPermission(value = "start:data:screen:config:info", name = "查看")
    public Result<?> getById(@PathVariable("id") String id) {
        return Result.ok(sysFormConfigService.getById(id));
    }


    @DeleteMapping("/deleteById/{id}")
    @PreAuthorize("@pas.hasPermission('start:data:screen:config:delete')")
    @RegisterPermission(value = "start:data:screen:config:delete", name = "删除")
    public Result<?> deleteById(@PathVariable("id") String id) {
        sysFormConfigService.removeById(id);
        return Result.ok();
    }


}
